--[[
	桌子内聊天，发表情， 暂时是客户端来给，应该给那些rid发广播，简单一些
	以后应该要判断是在桌子里，然后向游戏服要桌子里的玩家列表，再发送。
]]
local filename = "tablechat.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"

local json = require "cjson"
json.encode_sparse_array(true,1,1)


local service = nil
local CMD = {
	service = nil
}

function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
	-- if service.gamenode == "" or service.table_index <= 0  then return false end
	if msgbody.propid ~=nil and msgbody.propid ~= 0 then
		---判断道具类型
		local kid = math.floor(msgbody.propid / 100)
		if kid ~= PropKind.INTERPROP then
			return { retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "道具参数错误"}
		end
		local prop_cfg = skynet.call(".knapsack", "lua", "getpropconf", msgbody.propid)
		if not prop_cfg then
			return { retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "道具参数不存在"}
		end
		local status, propitem = skynet.pcall(skynet.call, ".knapsack", "lua", "getpropbyid", rid, msgbody.propid)
		if not propitem or propitem.value <= 0 then
			if not prop_cfg.price or prop_cfg.price == -1 then
				return { retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "该表情不能直接购买" }
			end
			----检查钻石是否足够
			if service.money.diamond < prop_cfg.price then
				return { retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "金豆不足" }
			end
			local begin_value = service.money.diamond
			local itemvalues = { }
			itemvalues["diamond"] = - tonumber(prop_cfg.price)
			local setargs = service:change_money(itemvalues)
			if setargs then
				----记录日志
				local logstr = nil
        		local comment = { }
        		local now = timetool.get_time()
				comment.tradeid = skynet.call(".nodeuuid", "lua", "getuuid")
				comment.table_uuid = "buyemojinode" .. "_" .. tostring(msgbody.propid) .. "_" .. tostring(prop_cfg.price) .. "_" .. now
				logstr = string.format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s", EDiamondChange.BUYEMOJI, rid, service.info.level, -tonumber(prop_cfg.price), 0, 0, begin_value, service.money.diamond, service.money.lock_diamond, comment.table_uuid, now, comment.tradeid)
            	skynet.send(".diamondlog", "lua", "write_file_redis", logstr, rid)
            	--在游戏内使用，通知游戏节点
            	if service.gamenode ~= "" then
			    	skynet.pcall(cluster.send, service.gamenode, ".router", "rid_change_diamond", rid, begin_value, -tonumber(prop_cfg.price), service.money.diamond)
		    	end
			end
		else
			-----扣除表情
			propitem.value = propitem.value - 1
			local ntclist = {}
			table.insert(ntclist, { id = propitem.id, value = propitem.value})
			local updatelist = {}
			propitem.value = -1
			table.insert(updatelist, propitem)
			---更新数据库
			local result = skynet.call(".knapsack", "lua", "updateknapsack", rid, updatelist)
    		if not result then
        		return {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "发送表情失败"}
			end
			--通知玩家,背包信息改变
			service:notify_client("changepropN", { change_prop_listjson = json.encode(ntclist) })
			if prop_cfg.broadcast_type == EBroadcastType.IN_TABLE_AND_HALL then
				--是广播的互动表情
				if msgbody.objrid ~= nil and msgbody.objrid ~= 0 then
					local objrid = tonumber(msgbody.objrid)
					local objagent = skynet.call(".mainnode", "lua", "cmd", "get_agent_by_rid", objrid)
					if objagent then
						local objinfo = skynet.call(objagent, "lua", "cmd", "myroleinfo")
						if objinfo then
							local data = { minename = service.info.rolename, othername = objinfo.rolename}
							cluster.send("gatenode", ".router", "addpopmessage", 1, data)
						end
					end
				end
			end
		end
		----更新玩家达人值
		local result = cluster.call("datanode", ".router", "select_rk", rid, "rs_drresult")
		local curscore = 0
		if result[1] ~= 0 then 
			curscore = result[2][rid].maxscore or 0
		end
		if prop_cfg.dr_value ~= nil and prop_cfg.dr_value > 0 then
			curscore = curscore + prop_cfg.dr_value
			cluster.send("datanode", ".router", "update_rk", rid, "rs_drresult", 0, {maxscore = curscore})
		end
	end
	local chatmsg = {}
	chatmsg.objrid		= msgbody.objrid
	chatmsg.chattype	= msgbody.chattype
	chatmsg.content		= msgbody.content
	chatmsg.talkrid		= rid
	-- 先给自己广播聊天消息
	service:notify_client("tablechatN", chatmsg)

	-- 再给桌内其他人广播
	local ridlist = msgbody.ridlist or {}

    -- 百人游戏从gate获取玩家列表
    if (not ridlist or #(ridlist) == 0) and service.gamenode then
        local nodename = string.sub(service.gamenode, 1, -5)
        local new_games = {lhd = true, brnn = true, ttz = true, rb = true, hbsl = true, bjl = true, bm = true, fqzs = true, yydb = true, 
        	slwh = true, sgj = true, dice = true, lp = true, dice3 = true, sss = true, pj = true, }
        if new_games[nodename] then
            ridlist = skynet.call(".roletablestate", "lua", "get_table_allrid", service.gamenode, service.table_index)
        end
    end

	local agent = nil
	for _,v in pairs(ridlist) do
		if v ~= rid then
			agent = skynet.call(".mainnode", "lua", "cmd", "get_agent_by_rid", v)
			if agent then
				skynet.send(agent, "lua", "cmd", "tablechatnotify", chatmsg)
			end
		end
	end
	return false
end

return CMD